/* $Id: AppMetaDataFacadeImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.facade.provider;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;


import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.ConstantsCommon.WFProcessType;
import ae.pcfc.etks.online.common.ConstantsCommon.WorkflowDepartment;
import ae.pcfc.etks.online.common.domain.AppMetaData;
import ae.pcfc.etks.online.common.domain.ProcStepAthmnt;
import ae.pcfc.etks.online.common.domain.Process;
import ae.pcfc.etks.online.common.domain.ProcessStep;
import ae.pcfc.etks.online.common.domain.TTscDepartments;
import ae.pcfc.etks.online.common.domain.manager.AppMetaDataDomainObjectManager;
import ae.pcfc.etks.online.common.domain.manager.ProcessDomainObjectManager;
import ae.pcfc.etks.online.common.domain.manager.UtilityDomainObjectManager;
import ae.pcfc.etks.online.common.facade.AppMetaDataFacade;
import ae.pcfc.etks.online.common.vo.AdminApprovalVO;
import ae.pcfc.etks.online.common.vo.AppMetaDataDetailVO;
import ae.pcfc.etks.online.common.vo.AppMetaDataVO;
import ae.pcfc.etks.online.common.vo.DepartmentVO;
import ae.pcfc.etks.online.common.vo.LabelVO;
import ae.pcfc.etks.online.common.vo.ProcessStepVO;
import ae.pcfc.etks.online.common.vo.StepCommentVO;
import ae.pcfc.etks.online.common.vo.UploadDocumentVO;
import ae.pcfc.etks.online.common.vo.WorkflowRequestSearchCriteriaVO;
import ae.pcfc.etks.online.common.vo.WorkflowRequestVO;

public class AppMetaDataFacadeImpl extends BaseFacade implements
		AppMetaDataFacade {
    /*
	 * Here we coordinate between the all interacting business domain objects
	 * (BO), transactions are maintained here. use case business validation are
	 * maintained here (that requires DB checks or retrieving data from DB)
	 */
	private AppMetaDataDomainObjectManager appMetaDataDomainObjectManager;
	private ProcessDomainObjectManager processDomainObjectManager;
	private UtilityDomainObjectManager utilityDomainObjectManager;

	public UtilityDomainObjectManager getUtilityDomainObjectManager() {
		return utilityDomainObjectManager;
	}

	public void setUtilityDomainObjectManager(
			UtilityDomainObjectManager utilityDomainObjectManager) {
		this.utilityDomainObjectManager = utilityDomainObjectManager;
	}

	public ProcessDomainObjectManager getProcessDomainObjectManager() {
		return processDomainObjectManager;
	}

	public void setProcessDomainObjectManager(
			ProcessDomainObjectManager processDomainObjectManager) {
		this.processDomainObjectManager = processDomainObjectManager;
	}

	public void setAppMetaDataDomainObjectManager(
			AppMetaDataDomainObjectManager appMetaDataDomainObjectManager) {
		this.appMetaDataDomainObjectManager = appMetaDataDomainObjectManager;
	}
	/**
	 * findAppMetaDataByPrimaryKey
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#findAppMetaDataByPrimaryKey(java.lang.String)
	 * 
	 */
	@Transactional(readOnly = true)
	public AdminApprovalVO findAppMetaDataByPrimaryKey(String applicationId) {

		AppMetaData appMetaData = this.appMetaDataDomainObjectManager
				.findByPrimaryKey(applicationId);
		AdminApprovalVO approvalVO = new AdminApprovalVO();
		if(appMetaData == null){
		    return null;
		}
		getMapper().map(appMetaData, approvalVO);
		approvalVO.setStrStatusAr(appMetaData.getWfStatusAr());
		approvalVO.setStrStatusEn(appMetaData.getWfStatusEn());
		approvalVO.setStrApplnStatus(appMetaData.getWfStatus());
		
/*		if (appMetaData != null) {
			if(null != approvalVO && approvalVO.getCommentsVOs()!= null){
				for(StepCommentVO comment : approvalVO.getCommentsVOs()){
					ProcessStepVO step = comment.getProcessStep();
					if(step != null){
						String deptCode = StringUtils.trim(StringUtils.remove(step.getProcessStepCode().toUpperCase(), ConstantsCommon.PROCESS_STEP_REVIEW).toUpperCase());
						if(deptCode != null && !org.apache.commons.lang.StringUtils.isEmpty(deptCode)){
							WorkflowDepartment dept = WorkflowDepartment.getWorkflowDepartmentfromString(deptCode);
							if(dept != null){
								TTscDepartments department = this.utilityDomainObjectManager.getDepartmentByCode(dept.getCode());
								DepartmentVO departmentVO = new DepartmentVO();
								if(department!=null){
								    this.getMapper().map(department, departmentVO);
								}
								step.setDepartmentNameAr(departmentVO.getStrDescDescA());
								step.setDepartmentNameEn(departmentVO.getStrDescDesc());
								step.setDepartmentCode(departmentVO.getStrDeptCode());
							}
						}
					}
				}
				
			}
		}
*/
		if (approvalVO.getUploadDocumentVOs() != null
				&& approvalVO.getUploadDocumentVOs().size() > 0
				&& appMetaData.getProcStepAthmnts() != null
				&& appMetaData.getProcStepAthmnts().size() > 0) {
			// int i = 0 ;
			for (ProcStepAthmnt athmnt : appMetaData.getProcStepAthmnts()) {

				for (UploadDocumentVO documentVO : approvalVO
						.getUploadDocumentVOs()) {
					/*if (athmnt.getAttachmentPath() != null
							&& athmnt.getAttachmentPath().endsWith(
									documentVO.getURL())) {*/
					if (athmnt.getAttachmentPath() != null
							&& athmnt.getAttachmentPath().endsWith(
									documentVO.getURL()) && athmnt.getAttachmentId()!= null && athmnt.getAttachmentId().equals(documentVO.getlFileId())) {
						if (athmnt.getAttachmentType() != null
								&& athmnt.getAttachmentType()
										.getAttachmentTypeCode() != null) {
							documentVO.setStrUploadDocumentType(athmnt
									.getAttachmentType()
									.getAttachmentTypeCode());
						}
					}
				}

				// approvalVO.getUploadDocumentVOs().get(i).setStrUploadDocumentType(athmnt.getAttachmentType().getAttachmentTypeCode());
				// ++i;
			}
		}

		return (approvalVO == null) ? null : approvalVO;
	}
	/**
	 * findAppMetaDataByUserId
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#findAppMetaDataByUserId(java.lang.Long)
	 * 
	 */
	@Transactional(readOnly = true)
	public List<AppMetaDataDetailVO> findAppMetaDataByUserId(Long id) {

		List<AppMetaData> appMetaDataList = this.appMetaDataDomainObjectManager
				.findAppMetaDataByUserId(id);
		List<AppMetaDataDetailVO> appMetaDataVOList = new ArrayList<AppMetaDataDetailVO>();
		AppMetaDataDetailVO appMetaDataVO;

		if (appMetaDataList != null && appMetaDataList.size() > 0) {
			for (AppMetaData appMetaData : appMetaDataList) {

				appMetaDataVO = new AppMetaDataDetailVO();
				getMapper().map(appMetaData, appMetaDataVO);
				appMetaDataVO.setProcessCode(WFProcessType
						.getWFProcessTypefromString(appMetaData.getProcess()
								.getProcessCode()));
				appMetaDataVO.setStrProcessCodeDescAr(appMetaData.getProcess()
						.getProcessDescA());
				appMetaDataVO.setStrProcessCodeDescEn(appMetaData.getProcess()
						.getProcessDesc());
				appMetaDataVOList.add(appMetaDataVO);
			}
		}

		return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ? appMetaDataVOList
				: null;
	}
	/**
	 * findAppMetaDataByClientRefNo
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#findAppMetaDataByClientRefNo(java.lang.String)
	 * 
	 */
	@Transactional(readOnly = true)
	public AppMetaDataDetailVO findAppMetaDataByClientRefNo(String refNo) {
		AppMetaData appMetaData = this.appMetaDataDomainObjectManager
				.findByClientRefNo(refNo);
		AppMetaDataDetailVO appMetaDataVO;

		if (appMetaData != null) {
			appMetaDataVO = new AppMetaDataDetailVO();
			getMapper().map(appMetaData, appMetaDataVO);
			if(null != appMetaDataVO && appMetaDataVO.getCommentsVOs()!= null){
				for(StepCommentVO comment : appMetaDataVO.getCommentsVOs()){
					ProcessStepVO step = comment.getProcessStep();
					if(step != null){
						String deptCode = StringUtils.remove(step.getProcessStepCode(), ConstantsCommon.PROCESS_STEP_REVIEW + ConstantsCommon.PROCESS_STEP_CODE_CONNECTOR);
						if(deptCode != null && !org.apache.commons.lang.StringUtils.isEmpty(deptCode)){
							WorkflowDepartment dept = WorkflowDepartment.getWorkflowDepartmentfromString(deptCode);
							if(dept != null){
								TTscDepartments department = this.utilityDomainObjectManager.getDepartmentByCode(dept.getCode());
								DepartmentVO departmentVO = new DepartmentVO();
								this.getMapper().map(department, departmentVO);
								step.setDepartmentNameAr(departmentVO.getStrDescDescA());
								step.setDepartmentNameEn(departmentVO.getStrDescDesc());
							}
						}
					}
				}
				
			}
			appMetaDataVO.setProcessCode(WFProcessType
					.getWFProcessTypefromString(appMetaData.getProcess()
							.getProcessCode()));
			appMetaDataVO.setStrProcessCodeDescAr(appMetaData.getProcess()
					.getProcessDescA());
			appMetaDataVO.setStrProcessCodeDescEn(appMetaData.getProcess()
					.getProcessDesc());
			return appMetaDataVO;
		}
		return null;
	}

	/**
	 * findCompletedAppMetaDataByUserId
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#findCompletedAppMetaDataByUserId(java.lang.Long)
	 * 
	 */
	@Transactional(readOnly = true)
	public List<AppMetaDataDetailVO> findCompletedAppMetaDataByUserId(Long id) {

		List<AppMetaData> appMetaDataList = this.appMetaDataDomainObjectManager
				.findCompletedAppMetaDataByUserId(id);
		if (appMetaDataList != null && appMetaDataList.size() > 0) {
			List<AppMetaDataDetailVO> appMetaDataVOList = new ArrayList<AppMetaDataDetailVO>();
			AppMetaDataDetailVO appMetaDataVO;

			for (AppMetaData appMetaData : appMetaDataList) {

				appMetaDataVO = new AppMetaDataDetailVO();
				getMapper().map(appMetaData, appMetaDataVO);
				appMetaDataVO.setProcessCode(WFProcessType
						.getWFProcessTypefromString(appMetaData.getProcess()
								.getProcessCode()));
				appMetaDataVO.setStrProcessCodeDescAr(appMetaData.getProcess()
						.getProcessDescA());
				appMetaDataVO.setStrProcessCodeDescEn(appMetaData.getProcess()
						.getProcessDesc());
				appMetaDataVOList.add(appMetaDataVO);
			}

			return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ? appMetaDataVOList
					: null;
		}
		return null;
	}
	/**
	 * addApplicationRequest
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#addApplicationRequest(ae.pcfc.etks.online.common.vo.AppMetaDataVO)
	 * 
	 */
	@Transactional(readOnly = false)
	public void addApplicationRequest(AppMetaDataVO appMetaDataVO) {
		AppMetaData appMetaData = new AppMetaData();
		this.getMapper().map(appMetaDataVO, appMetaData);
		Process process = this.processDomainObjectManager
				.getProcessByProcessCode(appMetaDataVO.getProcessCode()
						.getProcessCode());
		appMetaData.setProcess(process);
		this.appMetaDataDomainObjectManager.add(appMetaData);
	}
	/**
	 * updateApplicationRequest
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#updateApplicationRequest(ae.pcfc.etks.online.common.vo.AppMetaDataVO)
	 * 
	 */
	@Transactional(readOnly = false)
	public void updateApplicationRequest(AppMetaDataVO appMetaDataVO) {
		AppMetaData appMetaData = this.appMetaDataDomainObjectManager
				.findByPrimaryKey(appMetaDataVO.getStrApplicationId());
		appMetaData.setWfStatusAr(appMetaDataVO.getStrStatusDescAr());
		appMetaData.setWfStatusEn(appMetaDataVO.getStrStatusDescEn());
		/*
		 * this.getMapper().map(appMetaDataVO, appMetaData); Process process
		 * =this
		 * .processDomainObjectManager.getProcessByProcessCode(appMetaDataVO
		 * .getProcessCode().getProcessCode()); appMetaData.setProcess(process);
		 */
		this.appMetaDataDomainObjectManager.update(appMetaData);
	}
	/**
	 * getApplicationRequest
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#getApplicationRequest(java.lang.String)
	 * 
	 */
	@Transactional(readOnly = true)
	public AppMetaDataVO getApplicationRequest(String applicationId) {
		AppMetaData appMetaData = this.appMetaDataDomainObjectManager
				.findByPrimaryKey(applicationId);
		AppMetaDataVO appMetaDataVO;

		if (appMetaData != null) {
			appMetaDataVO = new AppMetaDataVO();
			getMapper().map(appMetaData, appMetaDataVO);
			appMetaDataVO.setProcessCode(WFProcessType
					.getWFProcessTypefromString(appMetaData.getProcess()
							.getProcessCode()));
			// WFProcessType.valueOf(appMetaData
			// .getProcess().getProcessCode()));
			return appMetaDataVO;
		}
		return null;

	}
	/**
	 * updateApplicationStatus
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#updateApplicationStatus(java.lang.String, java.lang.String, java.lang.String)
	 * 
	 */
	@Transactional(readOnly = false)
	public void updateApplicationStatus(String applicationId, String statusEn,
			String statusAr) {
		this.appMetaDataDomainObjectManager.updateStatus(applicationId,
				statusEn, statusAr);
	}
	/**
	 * searchApplicationRequestsByProcessCode
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#searchApplicationRequestsByProcessCode(ae.pcfc.etks.online.common.ConstantsCommon.WFProcessType)
	 * 
	 */
	@Transactional(readOnly = true)
	public List<AppMetaDataVO> searchApplicationRequestsByProcessCode(
			WFProcessType processType) {
		List<AppMetaData> appMetaDataList = this.appMetaDataDomainObjectManager
				.findApplicationRequestsByProcessCode(processType);
		List<AppMetaDataVO> appMetaDataVOList = new ArrayList<AppMetaDataVO>();
		AppMetaDataVO appMetaDataVO;

		if (appMetaDataList != null && appMetaDataList.size() > 0) {
			for (AppMetaData appMetaData : appMetaDataList) {

				appMetaDataVO = new AppMetaDataVO();
				getMapper().map(appMetaData, appMetaDataVO);
				appMetaDataVO.setProcessCode(WFProcessType
						.getWFProcessTypefromString(appMetaData.getProcess()
								.getProcessCode()));
				appMetaDataVOList.add(appMetaDataVO);
			}
		}

		return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ? appMetaDataVOList
				: null;
	}
	/**
	 * searchApplicationRequestsByInitiator
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#searchApplicationRequestsByInitiator(java.lang.String)
	 * 
	 */
	@Transactional(readOnly = true)
	public List<AppMetaDataVO> searchApplicationRequestsByInitiator(
			String username) {
		List<AppMetaData> appMetaDataList = this.appMetaDataDomainObjectManager
				.findApplicationRequestsByInitiator(username);
		List<AppMetaDataVO> appMetaDataVOList = new ArrayList<AppMetaDataVO>();
		AppMetaDataVO appMetaDataVO;

		if (appMetaDataList != null && appMetaDataList.size() > 0) {
			for (AppMetaData appMetaData : appMetaDataList) {

				appMetaDataVO = new AppMetaDataVO();
				getMapper().map(appMetaData, appMetaDataVO);
				appMetaDataVO.setProcessCode(WFProcessType
						.getWFProcessTypefromString(appMetaData.getProcess()
								.getProcessCode()));
				appMetaDataVOList.add(appMetaDataVO);
			}
		}

		return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ? appMetaDataVOList
				: null;
	}
	/**
	 * searchApplicationRequestsByApplicationDate
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#searchApplicationRequestsByApplicationDate(java.lang.String)
	 * 
	 */
	@Transactional(readOnly = true)
	public List<AppMetaDataVO> searchApplicationRequestsByApplicationDate(
			String date) {
		List<AppMetaData> appMetaDataList = this.appMetaDataDomainObjectManager
				.findApplicationRequestsByApplicationDate(date);
		List<AppMetaDataVO> appMetaDataVOList = new ArrayList<AppMetaDataVO>();
		AppMetaDataVO appMetaDataVO;

		if (appMetaDataList != null && appMetaDataList.size() > 0) {
			for (AppMetaData appMetaData : appMetaDataList) {

				appMetaDataVO = new AppMetaDataVO();
				getMapper().map(appMetaData, appMetaDataVO);
				appMetaDataVO.setProcessCode(WFProcessType
						.getWFProcessTypefromString(appMetaData.getProcess()
								.getProcessCode()));
				appMetaDataVOList.add(appMetaDataVO);
			}
		}

		return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ? appMetaDataVOList
				: null;
	}

	/**
	 * searchApplicationRequestsByUserId
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#searchApplicationRequestsByUserId(java.lang.Long)
	 * 
	 */
	@Transactional(readOnly = true)
	public List<AppMetaDataVO> searchApplicationRequestsByUserId(Long userId) {
		List<AppMetaData> appMetaDataList = this.appMetaDataDomainObjectManager
				.findAppMetaDataByUserId(userId);
		List<AppMetaDataVO> appMetaDataVOList = new ArrayList<AppMetaDataVO>();
		AppMetaDataVO appMetaDataVO;

		if (appMetaDataList != null && appMetaDataList.size() > 0) {
			for (AppMetaData appMetaData : appMetaDataList) {

				appMetaDataVO = new AppMetaDataVO();
				getMapper().map(appMetaData, appMetaDataVO);
				appMetaDataVO.setProcessCode(WFProcessType
						.getWFProcessTypefromString(appMetaData.getProcess()
								.getProcessCode()));
				appMetaDataVOList.add(appMetaDataVO);
			}
		}

		return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ? appMetaDataVOList
				: null;
	}
	/**
	 * Description:
	 * findAppMetaDataByExample:	Method to get all the Application requests by an example VO
	 * This method acquires the AppMetaData domain objects for the user from the AppMetaDataDomainObjectManager	 * 
	 * @author Dicetek.Ismail
	 * @param	appMetaDataDetailVO AppMetaDataDetailVO	  Example VO with fields filled for filtering the results
	 * @return	- List<AppMetaData> - returns a list of AppMetaData
	 */	
	@Transactional(readOnly = true)
	private List<AppMetaData> findAppMetaDataByExample(
			AppMetaDataDetailVO appMetaDataDetailVO) {
		return this.appMetaDataDomainObjectManager
				.findAppMetaDataByExample(appMetaDataDetailVO);
	}
	/**
	 * findAppMetaDataVOByExample
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#findAppMetaDataVOByExample(ae.pcfc.etks.online.common.vo.AppMetaDataVO)
	 * 
	 */
	@Transactional(readOnly = true)
	public List<AppMetaDataVO> findAppMetaDataVOByExample(
			AppMetaDataVO appMetaDataVO) {
		if (appMetaDataVO != null) {
			AppMetaDataDetailVO appMetaDataDetailVO = new AppMetaDataDetailVO();
			getMapper().map(appMetaDataVO, appMetaDataDetailVO);
			List<AppMetaData> appMetaDataList = findAppMetaDataByExample(appMetaDataDetailVO);
			List<AppMetaDataVO> appMetaDataVOList = new ArrayList<AppMetaDataVO>();
			AppMetaDataVO appMetaDataVO1;

			if (appMetaDataList != null && appMetaDataList.size() > 0) {
				for (AppMetaData appMetaData : appMetaDataList) {

					appMetaDataVO1 = new AppMetaDataVO();
					getMapper().map(appMetaData, appMetaDataVO1);
					appMetaDataVO1.setProcessCode(WFProcessType
							.getWFProcessTypefromString(appMetaData
									.getProcess().getProcessCode()));
					appMetaDataVOList.add(appMetaDataVO1);
				}
			}

			return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ? appMetaDataVOList
					: null;

		}
		return null;
	}
	/**
	 * findAppMetaDataDetailVOByExample
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#findAppMetaDataDetailVOByExample(ae.pcfc.etks.online.common.vo.AppMetaDataDetailVO)
	 * 
	 */
	@Transactional(readOnly = true)
	public List<AppMetaDataDetailVO> findAppMetaDataDetailVOByExample(
			AppMetaDataDetailVO appMetaDataDetailVO) {
		// TODO Auto-generated method stub
		if (appMetaDataDetailVO != null) {
			List<AppMetaData> appMetaDataList = findAppMetaDataByExample(appMetaDataDetailVO);
			List<AppMetaDataDetailVO> appMetaDataVOList = new ArrayList<AppMetaDataDetailVO>();
			AppMetaDataDetailVO appMetaDataVO1;

			if (appMetaDataList != null && appMetaDataList.size() > 0) {
				for (AppMetaData appMetaData : appMetaDataList) {

					appMetaDataVO1 = new AppMetaDataDetailVO();
					getMapper().map(appMetaData, appMetaDataVO1);
					appMetaDataVO1.setProcessCode(WFProcessType
							.getWFProcessTypefromString(appMetaData
									.getProcess().getProcessCode()));
					appMetaDataVO1.setStrProcessCodeDescEn(appMetaData.getProcess().getProcessDesc());
					appMetaDataVO1.setStrProcessCodeDescAr(appMetaData.getProcess().getProcessDescA());
					appMetaDataVOList.add(appMetaDataVO1);
				}
			}

			return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ? appMetaDataVOList
					: null;
		}
		return null;
	}

	/**
	 * findAppMetaDataByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#findAppMetaDataByUserName(java.lang.String)
	 * 
	 */
	@Transactional(readOnly = true)
	public List<AppMetaDataDetailVO> findAppMetaDataByUserName(String strUserName) {
		List<AppMetaData> appMetaDataList = this.appMetaDataDomainObjectManager
				.findAppMetaDataByUserName(strUserName);
		List<AppMetaDataDetailVO> appMetaDataVOList = new ArrayList<AppMetaDataDetailVO>();
		AppMetaDataDetailVO appMetaDataVO;

		if (appMetaDataList != null && appMetaDataList.size() > 0) {
			for (AppMetaData appMetaData : appMetaDataList) {

				appMetaDataVO = new AppMetaDataDetailVO();
				getMapper().map(appMetaData, appMetaDataVO);
				appMetaDataVO.setProcessCode(WFProcessType
						.getWFProcessTypefromString(appMetaData.getProcess()
								.getProcessCode()));
				appMetaDataVO.setStrProcessCodeDescAr(appMetaData.getProcess()
						.getProcessDescA());
				appMetaDataVO.setStrProcessCodeDescEn(appMetaData.getProcess()
						.getProcessDesc());
				appMetaDataVOList.add(appMetaDataVO);
			}
		}

		return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ? appMetaDataVOList
				: null;
	}

	/**
	 * findByIncidentIDAndProcessCode
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#findByIncidentIDAndProcessCode(java.lang.String, java.lang.String)
	 * 
	 */
	@Transactional(readOnly = true)
	public WorkflowRequestVO findByIncidentIDAndProcessCode(String incidentId,String strProcessCode, String strProcessStepCode) {
	    // TODO Auto-generated method stub
	    if(incidentId != null && strProcessCode != null){
		AppMetaDataDetailVO exampleVO = new AppMetaDataDetailVO();
		exampleVO.setStrWFIncidentId(incidentId);
		exampleVO.setProcessCode(WFProcessType.getWFProcessTypefromString(strProcessCode));
		exampleVO.setStrWfStatus(ConstantsCommon.IN_PROGRESS);
		List<AppMetaDataDetailVO> appMetaDataList = findAppMetaDataDetailVOByExample(exampleVO);
		if(appMetaDataList != null && appMetaDataList.size() > 0){
		    WorkflowRequestVO workflowRequestVO = new WorkflowRequestVO();
		    AppMetaDataDetailVO appMetaDataDetailVO = appMetaDataList.get(0);
		    this.getMapper().map(appMetaDataDetailVO, workflowRequestVO);
		    workflowRequestVO.setStrProcessCode(appMetaDataDetailVO.getProcessCode().getProcessCode());
		    if(!org.apache.commons.lang.StringUtils.isBlank(strProcessStepCode)){
			ProcessStep processStep = this.processDomainObjectManager.getProcessStepByProcessCodeAndProcessStepCode(strProcessCode, strProcessStepCode);
			workflowRequestVO.setStrProcessStepDescEn(processStep.getProcessStepDesc());
			workflowRequestVO.setStrProcessStepDescAr(processStep.getProcessStepDescA());
			workflowRequestVO.setStrProcessStep(strProcessStepCode);
		    }
		    
		    return workflowRequestVO;
		}
	    }
	    return null;
	    
	}

	/**
	 * getNextApplicationId
	 * 
	 * @see ae.pcfc.etks.online.common.facade.AppMetaDataFacade#getNextApplicationId()
	 * 
	 */
	@Transactional
	public String getNextApplicationId() {
	    // TODO Auto-generated method stub
	    //throw new RuntimeException();
	    return this.appMetaDataDomainObjectManager.getNextApplicationId();
	}

	// public List<AppMetaDataDetailVO> findCompletedAppMetaDataByUserId(
	// AppMetaDataDetailVO appMetaDataDetailVO) {
	// // TODO Auto-generated method stub
	// List<AppMetaData> appMetaDataList =
	// this.appMetaDataDomainObjectManager.findCompletedAppMetaDataByUserId(appMetaDataDetailVO);
	// if (appMetaDataList != null && appMetaDataList.size() > 0) {
	// List<AppMetaDataDetailVO> appMetaDataVOList = new
	// ArrayList<AppMetaDataDetailVO>();
	// AppMetaDataDetailVO appMetaDataVO;
	//
	// for (AppMetaData appMetaData : appMetaDataList) {
	//
	// appMetaDataVO = new AppMetaDataDetailVO();
	// getMapper().map(appMetaData, appMetaDataVO);
	// appMetaDataVO.setProcessCode(WFProcessType.getWFProcessTypefromString(appMetaData.getProcess().getProcessCode()));
	// appMetaDataVO.setStrProcessCodeDescAr(appMetaData.getProcess()
	// .getProcessDescA());
	// appMetaDataVO.setStrProcessCodeDescEn(appMetaData.getProcess()
	// .getProcessDesc());
	// appMetaDataVOList.add(appMetaDataVO);
	// }
	//
	// return (appMetaDataVOList != null && appMetaDataVOList.size() > 0) ?
	// appMetaDataVOList
	// : null;
	// }
	// return null;
	// }

	@Transactional(readOnly = true)
	public List<WorkflowRequestVO> findWorkflowRequests(WorkflowRequestSearchCriteriaVO searchCriteria) {
		List<WorkflowRequestVO> requestsList = this.appMetaDataDomainObjectManager.findWorkflowRequests(searchCriteria);
		if(requestsList != null){
			return requestsList;
		}
		return null;
	}

	@Transactional(readOnly = true)
	public List<LabelVO> searchRequestStatus(String searchString) {
		List<LabelVO> requestsList = this.appMetaDataDomainObjectManager.searchRequestStatus(searchString);
		if(requestsList != null){
			return requestsList;
		}
		return null;
	}
}
